Variants allows to return a different data set for a given mocked route. Variants can be selected in the admin UI to determine what type of response a route should have. Routes are defined using the variant method on the Route object (returned by calling the route method). An object parameter is provided with the following attributes
Variants are useful because they allow you to test multiple scenarios that can happen with your route. Say, for example, you have a route exposing the ability to update a password. You might have several exceptional scenarios that you would want to test out (each could be a variant that you simply select to tell the route handler to use the appropriate response)
The handler defined under route is the default handler
midway.route({
id: 'helloWorld',
label: 'Hello World',
path: '/helloWorld',
method: 'GET',
handler: function (req, reply) {
reply('Hello World');
}
});
midway.route({
id: 'message',
label: 'Message',
path: '/get/message',
method: 'GET',
variantLabel: 'Hello',
handler: function (req, reply) {
reply('Hello');
}
})
.variant({
id: 'hello',
label: 'Hello World',
handler: function (req, reply) {
reply('Hello World');
}
});
You can select a different variant from admin UI to determine what type of response a route should have.
midway.route({
id: 'message',
label: 'Message',
path: '/get/message',
method: 'GET',
variantLabel: 'Hello',
handler: function (req, reply) {
reply('Hello');
}
})
.variant({
id: 'hello',
label: 'Hello World',
handler: function (req, reply) {
reply('Hello World');
}
})
.variant({
id: 'helloUniverse',
label: 'Hello Universe',
handler: function (req, reply) {
reply('Hello Universe');
}
});